package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.CureMedicineDAO;
import cn.edu.scau.cmi.hombio.dao.CureMethodDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineDAO;
import cn.edu.scau.cmi.hombio.dao.UnitDAO;

import cn.edu.scau.cmi.hombio.domain.CureMedicine;
import cn.edu.scau.cmi.hombio.domain.CureMethod;
import cn.edu.scau.cmi.hombio.domain.Medicine;
import cn.edu.scau.cmi.hombio.domain.Unit;

import cn.edu.scau.cmi.hombio.service.CureMedicineService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for CureMedicine entities
 * 
 */

@Controller("CureMedicineRestController")

public class CureMedicineRestController {

	/**
	 * DAO injected by Spring that manages CureMedicine entities
	 * 
	 */
	@Autowired
	private CureMedicineDAO cureMedicineDAO;

	/**
	 * DAO injected by Spring that manages CureMethod entities
	 * 
	 */
	@Autowired
	private CureMethodDAO cureMethodDAO;

	/**
	 * DAO injected by Spring that manages Medicine entities
	 * 
	 */
	@Autowired
	private MedicineDAO medicineDAO;

	/**
	 * DAO injected by Spring that manages Unit entities
	 * 
	 */
	@Autowired
	private UnitDAO unitDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for CureMedicine entities
	 * 
	 */
	@Autowired
	private CureMedicineService cureMedicineService;

	/**
	 * View an existing CureMethod entity
	 * 
	 */
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/cureMethod/{curemethod_id}", method = RequestMethod.GET)
	@ResponseBody
	public CureMethod loadCureMedicineCureMethod(@PathVariable Integer curemedicine_id, @PathVariable Integer related_curemethod_id) {
		CureMethod curemethod = cureMethodDAO.findCureMethodByPrimaryKey(related_curemethod_id, -1, -1);

		return curemethod;
	}

	/**
	* Show all CureMedicine entities
	* 
	*/
	@RequestMapping(value = "/CureMedicine", method = RequestMethod.GET)
	@ResponseBody
	public List<CureMedicine> listCureMedicines() {
		return new java.util.ArrayList<CureMedicine>(cureMedicineService.loadCureMedicines());
	}

	/**
	* Get Medicine entity by CureMedicine
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/medicine", method = RequestMethod.GET)
	@ResponseBody
	public Medicine getCureMedicineMedicine(@PathVariable Integer curemedicine_id) {
		return cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicine_id).getMedicine();
	}

	/**
	* Save an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/unit", method = RequestMethod.PUT)
	@ResponseBody
	public Unit saveCureMedicineUnit(@PathVariable Integer curemedicine_id, @RequestBody Unit unit) {
		cureMedicineService.saveCureMedicineUnit(curemedicine_id, unit);
		return unitDAO.findUnitByPrimaryKey(unit.getId());
	}

	/**
	* Create a new CureMethod entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/cureMethod", method = RequestMethod.POST)
	@ResponseBody
	public CureMethod newCureMedicineCureMethod(@PathVariable Integer curemedicine_id, @RequestBody CureMethod curemethod) {
		cureMedicineService.saveCureMedicineCureMethod(curemedicine_id, curemethod);
		return cureMethodDAO.findCureMethodByPrimaryKey(curemethod.getId());
	}

	/**
	* Save an existing CureMethod entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/cureMethod", method = RequestMethod.PUT)
	@ResponseBody
	public CureMethod saveCureMedicineCureMethod(@PathVariable Integer curemedicine_id, @RequestBody CureMethod curemethod) {
		cureMedicineService.saveCureMedicineCureMethod(curemedicine_id, curemethod);
		return cureMethodDAO.findCureMethodByPrimaryKey(curemethod.getId());
	}

	/**
	* Delete an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/unit/{unit_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteCureMedicineUnit(@PathVariable Integer curemedicine_id, @PathVariable Integer related_unit_id) {
		cureMedicineService.deleteCureMedicineUnit(curemedicine_id, related_unit_id);
	}

	/**
	* Delete an existing CureMethod entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/cureMethod/{curemethod_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteCureMedicineCureMethod(@PathVariable Integer curemedicine_id, @PathVariable Integer related_curemethod_id) {
		cureMedicineService.deleteCureMedicineCureMethod(curemedicine_id, related_curemethod_id);
	}

	/**
	* Create a new Medicine entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/medicine", method = RequestMethod.POST)
	@ResponseBody
	public Medicine newCureMedicineMedicine(@PathVariable Integer curemedicine_id, @RequestBody Medicine medicine) {
		cureMedicineService.saveCureMedicineMedicine(curemedicine_id, medicine);
		return medicineDAO.findMedicineByPrimaryKey(medicine.getId());
	}

	/**
	* Save an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/medicine", method = RequestMethod.PUT)
	@ResponseBody
	public Medicine saveCureMedicineMedicine(@PathVariable Integer curemedicine_id, @RequestBody Medicine medicine) {
		cureMedicineService.saveCureMedicineMedicine(curemedicine_id, medicine);
		return medicineDAO.findMedicineByPrimaryKey(medicine.getId());
	}

	/**
	* Create a new Unit entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/unit", method = RequestMethod.POST)
	@ResponseBody
	public Unit newCureMedicineUnit(@PathVariable Integer curemedicine_id, @RequestBody Unit unit) {
		cureMedicineService.saveCureMedicineUnit(curemedicine_id, unit);
		return unitDAO.findUnitByPrimaryKey(unit.getId());
	}

	/**
	* View an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/medicine/{medicine_id}", method = RequestMethod.GET)
	@ResponseBody
	public Medicine loadCureMedicineMedicine(@PathVariable Integer curemedicine_id, @PathVariable Integer related_medicine_id) {
		Medicine medicine = medicineDAO.findMedicineByPrimaryKey(related_medicine_id, -1, -1);

		return medicine;
	}

	/**
	* Select an existing CureMedicine entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}", method = RequestMethod.GET)
	@ResponseBody
	public CureMedicine loadCureMedicine(@PathVariable Integer curemedicine_id) {
		return cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicine_id);
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Create a new CureMedicine entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine", method = RequestMethod.POST)
	@ResponseBody
	public CureMedicine newCureMedicine(@RequestBody CureMedicine curemedicine) {
		cureMedicineService.saveCureMedicine(curemedicine);
		return cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicine.getId());
	}

	/**
	* View an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/unit/{unit_id}", method = RequestMethod.GET)
	@ResponseBody
	public Unit loadCureMedicineUnit(@PathVariable Integer curemedicine_id, @PathVariable Integer related_unit_id) {
		Unit unit = unitDAO.findUnitByPrimaryKey(related_unit_id, -1, -1);

		return unit;
	}

	/**
	* Delete an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/medicine/{medicine_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteCureMedicineMedicine(@PathVariable Integer curemedicine_id, @PathVariable Integer related_medicine_id) {
		cureMedicineService.deleteCureMedicineMedicine(curemedicine_id, related_medicine_id);
	}

	/**
	* Get CureMethod entity by CureMedicine
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/cureMethod", method = RequestMethod.GET)
	@ResponseBody
	public CureMethod getCureMedicineCureMethod(@PathVariable Integer curemedicine_id) {
		return cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicine_id).getCureMethod();
	}

	/**
	* Delete an existing CureMedicine entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteCureMedicine(@PathVariable Integer curemedicine_id) {
		CureMedicine curemedicine = cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicine_id);
		cureMedicineService.deleteCureMedicine(curemedicine);
	}

	/**
	* Save an existing CureMedicine entity
	* 
	*/
	@RequestMapping(value = "/CureMedicine", method = RequestMethod.PUT)
	@ResponseBody
	public CureMedicine saveCureMedicine(@RequestBody CureMedicine curemedicine) {
		cureMedicineService.saveCureMedicine(curemedicine);
		return cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicine.getId());
	}

	/**
	* Get Unit entity by CureMedicine
	* 
	*/
	@RequestMapping(value = "/CureMedicine/{curemedicine_id}/unit", method = RequestMethod.GET)
	@ResponseBody
	public Unit getCureMedicineUnit(@PathVariable Integer curemedicine_id) {
		return cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicine_id).getUnit();
	}
}